#region (c) 2009 Abdullin.com and Contributors

// Copyright (c) 2009 Abdullin.com and Contributors
// Site: http://abdullin.com/ncouch
// This code is released under the terms of the new BSD licence

#endregion

using System;
using System.Collections.Generic;
using NCouch.Error;
using NCouch.Rest;

namespace NCouch
{
	public abstract class CouchAdapterBase
	{
		readonly CouchRestClient _client;
		readonly List<RestHeader> _headers = new List<RestHeader>();
		

		void ApplyHeaders(CouchRequestSyntax syntax)
		{
			syntax.WithHeaders(_headers);
		}

		protected CouchAdapterBase(CouchRestClient client)
		{
			_client = client;
		}

		protected IRestResponse Put(string query, Action<CouchRequestSyntax> config)
		{
			return _client.GetRequest(CouchRestMethod.PUT, query, config + ApplyHeaders);
		}

		protected IRestResponse Put(string query)
		{
			return _client.GetRequest(CouchRestMethod.PUT, query, ApplyHeaders);
		}

		protected IRestResponse Get(string query)
		{
			return _client.GetRequest(CouchRestMethod.GET, query, ApplyHeaders);
		}

		protected IRestResponse Post(string query, Action<CouchRequestSyntax> config)
		{
			return _client.GetRequest(CouchRestMethod.POST, query, config + ApplyHeaders);
		}

		protected IRestResponse Delete(string query)
		{
			return _client.GetRequest(CouchRestMethod.DELETE, query, ApplyHeaders);
		}


		protected T ReadObject<T>(IRestResponse response)
		{
			return _client.Deserialize<T>(response);
		}

		public void AddHeader(string header, string value)
		{
			_headers.Add(new RestHeader(header, value));
		}

		protected CouchErrorSyntax Fail(IRestResponse response)
		{
			return new CouchErrorSyntax(response);
		}
	}
}